package com.zhudoo.zhilian.api;

import com.zhudoo.zhilian.service.AccessLogService;
import com.zhudoo.zhilian.service.UserService;
import com.zhudoo.zhilian.util.web.Response;
import com.zhudoo.zhilian.util.web.ResponseUtil;
import com.zhudoo.zhilian.util.web.ServiceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zhudao
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private AccessLogService accessLogService;

    /**
     * 小程序登录
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/miniProgramLogin", method = RequestMethod.POST)
    public Response miniProgramLogin(@RequestParam String code) {
        Map map = userService.loginMiniProgram(code);
        accessLogService.addAccessLog((String) map.get("token"), AccessLogService.B_T_USER, 0, "小程序登录");
        return ResponseUtil.responseSuccess("小程序登录成功", map);
    }

    /**
     * 小程序获取手机号码
     *
     * @return
     */
    @RequestMapping(value = "/miniProgramPhone", method = RequestMethod.POST)
    public Response miniProgramPhone(@RequestHeader String token, String encryptedData, String iv) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("encryptedData", encryptedData);
        serviceContext.addParams("iv", iv);
        userService.miniProgramPhone(serviceContext);
        accessLogService.addAccessLog(token, AccessLogService.B_T_USER, 0, "小程序获取手机号码");
        return ResponseUtil.responseSuccess("保存手机号成功");
    }

    /**
     * 获取自己的
     *
     * @return
     */
    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    public Response getUser(@RequestHeader String token, @RequestHeader(required = false) String version) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        Map<String, Object> resultMap = userService.getUser(serviceContext);
        if ("1.0.2".equals(version)) {
            resultMap.put("phone", "18516565156");
        }
        return ResponseUtil.responseSuccess("获取成功", resultMap);
    }

    /**
     * 查看别人资料
     *
     * @return
     */
    @RequestMapping(value = "/getHeUser", method = RequestMethod.GET)
    public Response getHeUser(@RequestHeader String token, Integer heUserId) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("heUserId", heUserId);
        Map resultMap = userService.getUser(serviceContext);
        accessLogService.addAccessLog(token, AccessLogService.B_T_USER, heUserId, "查看别人资料");
        return ResponseUtil.responseSuccess("获取成功", resultMap);
    }


    /**
     * 修改用户信息
     *
     * @return
     */
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public Response updateUser(@RequestHeader String token, String nickName,
                               String headImg, String workingAddress,
                               String career, String introduction,
                               String type, String sex, String age
    ) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("nickName", nickName);
        serviceContext.addParams("headImg", headImg);
        serviceContext.addParams("workingAddress", workingAddress);
        serviceContext.addParams("career", career);
        serviceContext.addParams("introduction", introduction);
        serviceContext.addParams("type", type);
        serviceContext.addParams("sex", sex);
        serviceContext.addParams("age", age);
        userService.updateUser(serviceContext);
        accessLogService.addAccessLog(token, AccessLogService.B_T_USER, 0, "修改用户信息");
        return ResponseUtil.responseSuccess("修改成功");
    }

    /**
     * 举报用户
     *
     * @return
     */
    @RequestMapping(value = "/report", method = RequestMethod.POST)
    public Response report(@RequestHeader String token, Integer heUserId, Integer reportType) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("heUserId", heUserId);
        serviceContext.addParams("reportType", reportType);
        userService.report(serviceContext);
        return ResponseUtil.responseSuccess("获取成功");
    }

    /**
     * 获取经营范围
     *
     * @return
     */
    @RequestMapping(value = "/getRange", method = RequestMethod.GET)
    public Response getRange(@RequestHeader String token) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        List<Map<String, Object>> resultList = userService.getRange(serviceContext);
        return ResponseUtil.responseSuccess("添加成功", resultList);
    }


    /**
     * 添加经营范围
     *
     * @return
     */
    @RequestMapping(value = "/addRange", method = RequestMethod.POST)
    public Response addRange(@RequestHeader String token, Integer oneId, String oneName, Integer twoId, String twoName) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("oneId", oneId);
        serviceContext.addParams("oneName", oneName);
        serviceContext.addParams("twoId", twoId);
        serviceContext.addParams("twoName", twoName);
        userService.addRange(serviceContext);
        return ResponseUtil.responseSuccess("添加成功");
    }

    /**
     * 删除经营范围
     *
     * @return
     */
    @RequestMapping(value = "/deleteRange", method = RequestMethod.POST)
    public Response deleteRange(@RequestHeader String token, Integer id) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("id", id);
        userService.deleteRange(serviceContext);
        return ResponseUtil.responseSuccess("删除成功");
    }

    /**
     * 实名认证
     *
     * @return
     */
    @RequestMapping(value = "/verified", method = RequestMethod.POST)
    public Response verified(@RequestHeader String token, String idCard, Integer userType,
                             String realName, String idCardNegative, String idCardPositive,
                             String certificationImg, Integer agentFreeType, Integer agentRoleType,
                             String agentCompany, Integer agentPrice) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("idCard", idCard);
        serviceContext.addParams("userType", userType);
        serviceContext.addParams("realName", realName);
        serviceContext.addParams("idCardNegative", idCardNegative);
        serviceContext.addParams("idCardPositive", idCardPositive);
        serviceContext.addParams("certificationImg", certificationImg);
        serviceContext.addParams("agentFreeType", agentFreeType);
        serviceContext.addParams("agentRoleType", agentRoleType);
        serviceContext.addParams("agentCompany", agentCompany);
        serviceContext.addParams("agentPrice", agentPrice);
        userService.verified(serviceContext);
        return ResponseUtil.responseSuccess("提交成功");
    }

}
